package org.tczs.rabbitmq.controller;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class DirectReceiver {

    @RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
    public void process(Channel channel, Message testMessage) {
        try{
            System.out.println("服务器1消息消息  : " + testMessage.toString());
            channel.basicAck(testMessage.getMessageProperties().getDeliveryTag(), true);
            int i = 1/0;
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //手动确认回滚 拒绝deliveryTag对应的消息，第二个参数是否requeue，true则重新入队列，否则丢弃或者进入死信队列。
                channel.basicReject(testMessage.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }

//    @RabbitHandler
//    @RabbitListener(queues = "TestDirectQueue")//监听的队列名称 TestDirectQueue
//    public void process2(Map testMessage) {
//        System.out.println("服务器2消息消息  : " + testMessage.toString());
//    }

}